package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements Operation {
    @Override
    public void work(BookList book) {
        System.out.println("增加图书...");
        int currentSize = book.getUsedsize();
        //1.判断书架(数组)是否存满
        if (currentSize == book.getBookArray().length) {
            System.out.println("书架已经存满了!");
            return;
        }
        //2.创建书对象
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入书名：");
        String name = sc.nextLine();
        System.out.println("请输入作者：");
        String author = sc.nextLine();
        System.out.println("请输入类型：");
        String type = sc.nextLine();
        System.out.println("请输入价格：");
        int price = sc.nextInt();
        //价格必须放在最后，因为整型输入后会调到下一行，跑到字符串输入的地方
        Book newBook = new Book(name,author,price,type);
        //3.判断书架中是否有这本书
        for (int i = 0; i < currentSize; i++) {
            String bookName = book.getBooks(i).getName();
            if (name.equals(bookName)) {
                System.out.println("已经存放了过这本书了！");
                return;
            }
        }
        //4.把书添加到数组中
        book.setBooks(currentSize,newBook);
        System.out.println("添加成功!");
        //5.更改书的存放数量
        book.setUsedsize(currentSize+1);
    }
}
